package com.ncloud.crm.dubboservice.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.crm.dubboservice.BsAttrInfoDubboService;
import com.ncloud.crm.entity.BsAttr;
import com.ncloud.crm.service.BsAttrService;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by shiqiang on 2017/7/7.
 */
@Service
public class BsAttrInfoDubboServiceImpl implements BsAttrInfoDubboService{
    private static final Logger logger = LogManager.getLogger(BsAttrInfoDubboService.class);

    @Autowired
    private BsAttrService bsAttrService;

    @Override
    public JSONObject getBsAttr(JSONObject obj) {
        if(obj == null) return new JSONObject();
        if(!obj.containsKey("id")){
            throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"参数id不存在！");
        }
        if(!obj.containsKey("attrType")){
            throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"参数attrType不存在！");
        }
        if(!obj.containsKey("attrCode")){
            throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"参数attrCode不存在！");
        }
        BsAttr bsAttr = new BsAttr();
        bsAttr.setId(obj.getLong("id"));
        bsAttr.setAttrCode(obj.getString("attrCode"));
        bsAttr.setAttrType(obj.getString("attrType"));
        BsAttr result = bsAttrService.getAttrInfo(bsAttr);
        return JSON.parseObject(JSON.toJSONString(result));
    }
}
